﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KL.Store.Web.Models;
using KL.Store.Core.Contact;
using KL.Store.Data;
using KL.Store.Core.Web;
using KL.Store.Core.Domain;

namespace KL.Store.Web.Controllers
{
    [Authorize]
    public class ContactController : KL.Store.Core.Web.BaseController
    {

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List()
        {
            return View();
        }

        [Child]
        public ActionResult ListGet(ContactListModel model)
        {
            string Search = "";
            if (model != null && !string.IsNullOrEmpty(model.Search))
            {
                Search = model.Search;
            }
            ContactListModel clModel = new ContactListModel();
            if (eStoreUser.AccountId > 0)
            {
                clModel.Contacts = dbContact.GetList(eStoreUser.AccountId, Search);
                if (clModel.Contacts == null) clModel.Contacts = new List<Contact>();
            }
            return PartialView("ListGet", clModel);
        }
        
        [Child]
        public ActionResult Delete(string Search, int id)
        {
            ContactListModel clModel = new ContactListModel();
            if (eStoreUser.AccountId > 0 && id > 0)
            {
                dbContact.Delete(eStoreUser.AccountId, id);
                clModel.Contacts = dbContact.GetList(eStoreUser.AccountId, Search);
                if (clModel.Contacts == null) clModel.Contacts = new List<Contact>();
            }
            return PartialView("ListGet", clModel);
        }

        public ActionResult Google(ContactModel contactModel)
        {
            //GoogleContactService.InitializeService(contactModel.UserName, contactModel.Password);
            contactModel.ContactList = GoogleContactService.GetAllContact(contactModel.UserName, contactModel.Password);
            foreach (var contact in contactModel.ContactList)
            {
                dbMember.Insert(contact.EmailAddress1, contact.Name, contact.Phone, eStoreUser.AccountId);
                if (!string.IsNullOrEmpty(contact.EmailAddress2))
                {
                    dbMember.Insert(contact.EmailAddress2, contact.Name, contact.Phone, eStoreUser.AccountId);
                }
            }
            return View("Index", contactModel);
        }

        public ActionResult Msn(string code)
        {
            string a = code;

            MsnContactService ms = new MsnContactService();

            List<MsnContact> contacts = ms.GetMsnContacts(code);

            foreach (var contact in contacts)
            {
                dbMember.Insert(contact.Email, string.Format("{0} {1}", contact.FirstName, contact.LastName), string.Empty, eStoreUser.AccountId);
            }

            return View("Index", new ContactModel { UserName = string.Empty, Password = string.Empty });
        }

        public ActionResult Other(string otherContacts)
        {
            string[] emails = otherContacts.Replace(" ", string.Empty).Split(";,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach (string email in emails)
            {
                if (KL.Store.Core.Utils.IsValidEmail(email))
                {
                    dbMember.Insert(email, string.Empty, string.Empty, eStoreUser.AccountId);
                }
            }

            return View("Index", new ContactModel { UserName = string.Empty, Password = string.Empty });
        }
    }
}
